package org.dtrd.modules.shop.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdProduct;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdProductPush;
import org.dtrd.modules.shop.entity.request.ProductPushRequest;
import org.dtrd.modules.shop.mapper.DtrdEntShcRdProductMapper;
import org.dtrd.modules.shop.service.IDtrdEntShcRdProductPushService;
import org.dtrd.modules.shop.service.IDtrdEntShcRdProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.shop.entity.bean.ProductDetail;
import org.dtrd.modules.shop.entity.bean.ProductListInfo;
import org.dtrd.modules.shop.entity.bean.ProductListRequest;
import org.dtrd.modules.shop.entity.bean.ProductPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.dtrd.modules.util.qrcode.QRCodeUtils.generateQRCodeAndUpLoad;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-07-23
 */
@DS("multi-datasource1")
@Service
@Slf4j
public class DtrdEntShcRdProductServiceImpl extends ServiceImpl<DtrdEntShcRdProductMapper, DtrdEntShcRdProduct> implements IDtrdEntShcRdProductService {

    private DtrdEntShcRdProductMapper dtrdEntShcRdProductMapper;
    @Autowired
    private IDtrdEntShcRdProductPushService productPushService;

    @Value("${dtrd.domain.shop}")
    private String shopPath;

    @Override
    public List<ProductListInfo> getProductList(ProductListRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdProduct> queryWrapper = baseQueryWrapper();
        List<DtrdEntShcRdProduct> poList = list(queryWrapper);
        List<ProductListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(ProductListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<ProductListInfo> pageProduct(ProductPageRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdProduct> queryWrapper = baseQueryWrapper();
        if (request.getProductCategory() != null && request.getProductCategory() != 0) {
            queryWrapper.eq(DtrdEntShcRdProduct::getProductCategory, request.getProductCategory());
        }
        queryWrapper.eq(DtrdEntShcRdProduct::getOrgCode, request.getOrgCode());
        IPage<DtrdEntShcRdProduct> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(ProductListInfo::new);
    }

    @Override
    public ProductDetail getProduct(Integer dataId) {
        DtrdEntShcRdProduct dtrdEntShcRdProduct = getById(dataId);
        return Optional.ofNullable(dtrdEntShcRdProduct)
                .map(po -> new ProductDetail().parseFromPo(po))
                .orElse(null);
    }

    @Override
    public boolean saveOrUpdateProduct(ProductDetail dto) {
        DtrdEntShcRdProduct dtrdEntShcRdProduct = new DtrdEntShcRdProduct();
        if (dto.getDataId() == null) {
            dtrdEntShcRdProduct = dtrdEntShcRdProduct.init();
        }
        dtrdEntShcRdProduct.parseFromDto(dto);
        return saveOrUpdate(dtrdEntShcRdProduct);
    }

    @Override
    public boolean removeProduct(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public boolean pushProductToPatient(ProductPushRequest pushRequest) {

        DtrdEntShcRdProductPush dtrdEntShcRdProductPush = new DtrdEntShcRdProductPush().parseFromDto(pushRequest);
        boolean save = productPushService.save(dtrdEntShcRdProductPush);
        if (save) {
            int dataId = dtrdEntShcRdProductPush.getDataId();
            String qrcodeUrl = generateQRCodeAndUpLoad(
                    shopPath + "/qrcode.html?content=order_qrcode|" + dataId,
                    System.currentTimeMillis() + ""
            );
            dtrdEntShcRdProductPush.setQrcodeUrl(qrcodeUrl);
            return productPushService.updateById(dtrdEntShcRdProductPush);
        }
        return false;
    }

    private LambdaQueryWrapper<DtrdEntShcRdProduct> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntShcRdProduct.class)
                .eq(DtrdEntShcRdProduct::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntShcRdProduct> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntShcRdProduct.class)
                .eq(DtrdEntShcRdProduct::getIsDel, 0);
    }
}
